package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.entity.EasyBuy_order;

public class EasyBuy_orderDaoImpl extends BaseDao<EasyBuy_order> implements EasyBuy_orderDao {
	Connection conn  = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	
	@Override
	public EasyBuy_order idSelectAll(int id) {
		EasyBuy_order order = null;
		conn = getConnection();
		try {
			ps = conn.prepareStatement("SELECT * FROM EasyBuy_order where userId = ?");
			ps.setObject(1,id);
			rs = ps.executeQuery();
			order= new EasyBuy_order();
			while (rs.next()) {
				order.setId(rs.getInt("id"));
				order.setUserId(rs.getInt("userId"));
				order.setLoginName(rs.getString("loginName"));
				order.setSerialNumber(rs.getString("serialNumber"));
				order.setUserAddress(rs.getString("userAddress"));
				order.setCost(rs.getFloat("cost"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(conn, ps, rs);
		}
		return order;
	}

	@Override
	public List<EasyBuy_order> selectAllOrder(int id) {
		List<EasyBuy_order> list = new ArrayList<EasyBuy_order>();
		conn = getConnection();
		try {
			ps = conn.prepareStatement("SELECT c.loginName,serialNumber,userAddress,c.cost,`name` ,fileName,quantity,price,orderid,productId " + 
					" FROM easybuy_order_detail AS a " + 
					" LEFT JOIN easybuy_product b ON a.productId  = b.id " + 
					" JOIN easybuy_order AS c  ON c.id = a.orderId " + 
					" JOIN easybuy_user AS d ON d.id = c.userId	" + 
					" WHERE c.userId = ?");
			ps.setObject(1,id);
			rs = ps.executeQuery();
			
			while (rs.next()) {
				EasyBuy_order order= new EasyBuy_order();
				order.setLoginName(rs.getString("loginName"));
				order.setSerialNumber(rs.getString("serialNumber"));
				order.setUserAddress(rs.getString("userAddress"));
				order.setCost(rs.getFloat("cost"));
				order.setName(rs.getString("name"));
				order.setFileName(rs.getString("fileName"));
				order.setQuantity(rs.getInt("quantity"));
				order.setPrice(rs.getFloat("price"));
				list.add(order);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(conn, ps, rs);
		}
		return list;
	}

	@Override
	public String getTableName() {
		return "EasyBuy_order";
	}

}
